package com.haoran.dwz.common.security;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
import org.springframework.security.web.util.matcher.OrRequestMatcher;
import org.springframework.security.web.util.matcher.RequestMatcher;
import org.springframework.stereotype.Component;

@Component
public class ApiPathPattern {
	private Logger logger = LoggerFactory.getLogger(ApiPathPattern.class);
	
	/**
	 * 安全的需要鉴权的(通过用户名密码认证方式)
	 */
	@Value("${cors.pathPattern.security:/api/**}")
	private String securityPathPatterns;

	@Value("${cors.pathPattern.acl:/kyeopenapi/**}")
	private String aclPathPattern;
	/**
	 * 公开的免鉴权的api
	 */
	@Value("${cors.pathPatterns.public:/api/login,/api/init-root,/api/register}")
	private String[] publicPathPatterns;
	
	@Value("${usm.pathPatterns.statics:/**/*.css, /static/**, /statics/**,/dist/**, /**/*.js, /**/*.js.map, /**/*.ts, /**/*.css.map,/**/*.png, /**/*.gif, /**/*.jpg, /**/*.fco, /**/*.woff, /**/*.woff2, /**/*.font, /**/*.svg, /**/*.ttf, /*.ico }")
	private String[] staticsPathPatterns;

	private OrRequestMatcher securityRequestMatcher;

	private OrRequestMatcher publicRequestMatcher;
	
	private RequestMatcher staticsRequestMatcher;

	@PostConstruct
	public void initOrRequestMatcher() {
		
		List<RequestMatcher> securityRequestMatchers = new ArrayList<>();
		securityRequestMatchers.add(new AntPathRequestMatcher(getSecurityPathPatterns()));
		setSecurityRequestMatcher(new OrRequestMatcher(securityRequestMatchers));

		List<RequestMatcher> publicRequestMatchers = new ArrayList<>();
		String[] publicPathPatterns = getPublicPathPatterns();
		for (int i = 0; i < publicPathPatterns.length; i++) {
			RequestMatcher pathRequestMatcher = new AntPathRequestMatcher(publicPathPatterns[i]);
			publicRequestMatchers.add(pathRequestMatcher);
		}
		setPublicRequestMatcher(new OrRequestMatcher(publicRequestMatchers));
		
		List<RequestMatcher> staticUrl = new ArrayList<>();
		for (int i = 0; i < staticsPathPatterns.length; i++) {
			logger.debug("add staticsPathPatterns" + staticsPathPatterns[i]);
			RequestMatcher pathRequestMatcher = new AntPathRequestMatcher(staticsPathPatterns[i], "GET");
			staticUrl.add(pathRequestMatcher);
		}
		setStaticsRequestMatcher(new OrRequestMatcher(staticUrl));
	}

	public OrRequestMatcher getSecurityRequestMatcher() {
		return securityRequestMatcher;
	}

	public void setSecurityRequestMatcher(OrRequestMatcher securityRequestMatcher) {
		this.securityRequestMatcher = securityRequestMatcher;
	}

	public OrRequestMatcher getPublicRequestMatcher() {
		return publicRequestMatcher;
	}

	public void setPublicRequestMatcher(OrRequestMatcher publicRequestMatcher) {
		this.publicRequestMatcher = publicRequestMatcher;
	}

	public String getSecurityPathPatterns() {
		return securityPathPatterns;
	}

	public void setSecurityPathPatterns(String securityPathPatterns) {
		this.securityPathPatterns = securityPathPatterns;
	}

	public String[] getPublicPathPatterns() {
		return publicPathPatterns;
	}

	public void setPublicPathPatterns(String[] publicPathPatterns) {
		this.publicPathPatterns = publicPathPatterns;
	}

	public String[] getStaticsPathPatterns() {
		return staticsPathPatterns;
	}

	public void setStaticsPathPatterns(String[] staticsPathPatterns) {
		this.staticsPathPatterns = staticsPathPatterns;
	}

	public RequestMatcher getStaticsRequestMatcher() {
		return staticsRequestMatcher;
	}

	public void setStaticsRequestMatcher(RequestMatcher staticsRequestMatcher) {
		this.staticsRequestMatcher = staticsRequestMatcher;
	}

	public String getAclPathPattern() {
		return aclPathPattern;
	}

	public void setAclPathPattern(String aclPathPattern) {
		this.aclPathPattern = aclPathPattern;
	}
	 
}
